<?php 
namespace app\api\controller;
use think\Db;
class Turnover extends Base{
	public function getList(){
		$page = $this->request->param('currentPage',1);
		$limit = $this->request->param('pageSize',10);
		$date1 = $this->request->param('date1','','trim');
		$date2 = $this->request->param('date2','','trim');
		$books_id = $this->request->param('books_id','','trim');
		$account_id = $this->request->param('account_id','','trim');
		$where = null;

		if( $date1 && $date2 && $account_id && $books_id){
			$d1 = strtotime($date1);
			$d2 = strtotime($date2);
			$where = 't.books_id = '.$books_id.' AND t.account_id = '.$account_id.' AND t.create_time BETWEEN '.$d1.' AND '.$d2;
		}
		if( $date1 && $date2 && !$account_id && $books_id){
			$d1 = strtotime($date1);
			$d2 = strtotime($date2);
			$where = 't.books_id = '.$books_id.' AND t.create_time BETWEEN '.$d1.' AND '.$d2;
		}
		if( $date1 && !$date2 && $account_id && $books_id){
			$d1 = strtotime($date1);
			$where = 't.books_id = '.$books_id.' AND t.account_id = '.$account_id.' AND t.create_time >= '.$d1;
		}
		if( $date1 && !$date2 && !$account_id && $books_id){
			$d1 = strtotime($date1);
			$where = 't.books_id = '.$books_id.' AND t.create_time >= '.$d1;
		}
		if( !$date1 && $date2 && $account_id && $books_id){
			$d2 = strtotime($date2);
			$where = 't.books_id = '.$books_id.' AND t.account_id = '.$account_id.' AND t.create_time <= '.$d2;
		}
		if( !$date1 && $date2 && !$account_id && $books_id){
			$d2 = strtotime($date2);
			$where = 't.books_id = '.$books_id.' AND t.create_time <= '.$d2;
		}
		if( !$date1 && !$date2 && $account_id && $books_id){
			$where = 't.books_id = '.$books_id.' AND t.account_id = '.$account_id;
		}
		if( !$date1 && !$date2 && !$account_id && $books_id){
			$where = 't.books_id = '.$books_id;
		}

		$list = Db::table('turnover')
				->alias('t')
				->field("t.create_time,t.id,t.`explain`,t.money,b.`name` AS books_name,c.`name` AS category_name,a.`name` AS account_name,bl.money AS bl_money")
				->where($where)
				->join(['account'=>'a'],'t.account_id = a.id')
				->join(['books'=>'b'],'t.books_id = b.id')
				->join(['category'=>'c'],'t.category_id = c.id')
				->join(['balance'=>'bl'],'bl.turnover_id = t.id')
				->page($page,$limit)
				->order('id','DESC')
				->select();
		$count = Db::table('turnover')
				->alias('t')
				->field("t.create_time,t.id,t.`explain`,t.money,b.`name` AS books_name,c.`name` AS category_name,a.`name` AS account_name,bl.money AS bl_money")
				->where($where)
				->join(['account'=>'a'],'t.account_id = a.id')
				->join(['books'=>'b'],'t.books_id = b.id')
				->join(['category'=>'c'],'t.category_id = c.id')
				->join(['balance'=>'bl'],'bl.turnover_id = t.id')
				->count();
		// $list = db('turnover')->where($where)->page($page,$limit)->order(['create_time','id'=>'asc'])->select();
		// $count = db('turnover')->where($where)->count();

		foreach ($list as $key => $value) {
			$list[$key]['create_time_str'] = date("Y-m-d H:i:s",$value['create_time']);
			$list[$key]['money'] = number_format($value['money'],2);
			$list[$key]['bl_money'] = number_format($value['bl_money'],2);
		}
		$data['status'] = 1;
		$data['data'] = $list;
		$data['count'] = $count;
		$data['msg'] = Db::getLastSql();

		return returnJson(200,$data);
	}

	public function getInitList(){
		$books_id = $this->request->param('books_id','trim');
		$account_list = db('account')->where('books_id = '.$books_id.' AND initial_money > 0')->select();
		$category_list = db('category')->where('books_id = '.$books_id)->select();
		$arr = array();
		foreach ($category_list as $key => $value) {
			// $category_list[$key]['child'] = db('account')->where('pid = '.$value['id'])->count();
			$count = db('category')->where('pid = '.$value['id'])->count();
			if($count==0){
				array_push($arr,$value);
			}
		}
		$data['status'] = 1;
		$data['account_list'] = $account_list;
		// $data['category_list'] = $category_list;
		$data['arr'] = $arr;
		return returnJson(200,$data);
	}

	public function add(){
		if($this->request->isPost()||$this->request->isAjax()){
			Db::startTrans();
			try{
				$add['books_id'] = $this->request->param('books_id','trim');
				$add['account_id'] = $this->request->param('account_id','trim');
				$add['category_id'] = $this->request->param('category_id','trim');
				$add['explain'] = $this->request->param('explain','trim');
				$add['money'] = $this->request->param('money','trim');
				$add['user_id'] = $this->user['id'];
				$add['create_time'] = time();
				//获取余额
				$balance = db('account')->where('id = '.$add['account_id'])->value('balance');
				//算出余额
				$balance = $balance + $add['money'];
				//插入流水表
				$res1 = Db::table('turnover')->insertGetId($add);
				//修改科目表余额
				$upd['id'] = $add['account_id'];
				$upd['balance'] = $balance;
				$res2 = Db::table('account')->update($upd);
				//插入余额表
				$add2['books_id'] = $add['books_id'];
				$add2['account_id'] = $add['account_id'];
				$add2['turnover_id'] = $res1;
				$add2['money'] = $balance;
				$add2['create_time'] = time();
				$res3 = Db::table('balance')->insert($add2);
				if($res1 > 0 && $res2 !== false && $res3 !== false){
					Db::commit();
					$data['status'] = 1;
					$data['msg'] = '添加成功！';
					return returnJson(200,$data);
				}else{
					Db::rollback();
					$data['status'] = 0;
					$data['msg'] = '添加失败！';
					return returnJson(200,$data);
				}
			}catch(\ErrorException $e){
				Db::rollback();
				$data['status'] = 0;
				$data['msg'] = '添加失败！';
				return returnJson(200,$data);
			}
		}
	}
}